package com.niiwoo.civet.mobile.lanmao.handler;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.niiwoo.civet.account.dto.request.UnbindCompanyBankCardCallbackRequestDTO;
import com.niiwoo.civet.account.dto.response.UnbindBankCardCallbackResponseDTO;
import com.niiwoo.civet.account.service.bankcard.BankCardDubboService;
import com.niiwoo.tripod.lanmao.enums.LanMaoGatewayServiceNameEnum;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class UnbindBankCardNotifyHandler implements LanMaoNotifyHandler {

    public static final Logger logger = LoggerFactory.getLogger(UnbindBankCardNotifyHandler.class);


    @Reference(version = "1.0.0")
    BankCardDubboService bankCardDubboService;

    @Override
    public boolean supports(String serviceName) {
        return LanMaoGatewayServiceNameEnum.UNBIND_BANKCARD.name().equals(serviceName);
    }

    @Override
    public HandleResult handle(JSONObject json) {
        logger.info("企业解绑银行卡回调,报文={}", json.toJSONString());
        UnbindBankCardGatewayResponse response = json.toJavaObject(UnbindBankCardGatewayResponse.class);
        HandleResult result = new HandleResult();
        UnbindBankCardCallbackResponseDTO unbindBankCardCallbackResponseDTO = null;
        try {
            UnbindCompanyBankCardCallbackRequestDTO unbindCompanyBankCardCallbackRequestDTO = new UnbindCompanyBankCardCallbackRequestDTO();
            unbindCompanyBankCardCallbackRequestDTO.setRequestNo(response.getRequestNo());
            unbindCompanyBankCardCallbackRequestDTO.setPlatformUserNo(response.getPlatformUserNo());
            unbindCompanyBankCardCallbackRequestDTO.setCode(response.getCode());
            unbindCompanyBankCardCallbackRequestDTO.setStatus(response.getStatus());
            unbindCompanyBankCardCallbackRequestDTO.setErrorCode(response.getErrorCode());
            unbindCompanyBankCardCallbackRequestDTO.setErrorMsg(response.getErrorMessage());

            logger.info("企业解绑银行卡回调处理,请求流水号={},请求参数={}", response.getRequestNo(), JSON.toJSONString(unbindCompanyBankCardCallbackRequestDTO));
            if (!response.isCallSuccess() || !response.isBusinessSuccess()) {
                unbindBankCardCallbackResponseDTO = bankCardDubboService.unbindCompanyBankCardCallbackFailed(unbindCompanyBankCardCallbackRequestDTO);
            } else {
                unbindBankCardCallbackResponseDTO = bankCardDubboService.unbindCompanyBankCardCallbackSuccess(unbindCompanyBankCardCallbackRequestDTO);
            }
            logger.info("企业解绑银行卡回调处理,请求流水号={},响应结果={}", response.getRequestNo(), JSON.toJSONString(unbindBankCardCallbackResponseDTO));

            result.setNeedAck(true);
            result.setRequestNo(response.getRequestNo());
            result.setData(unbindBankCardCallbackResponseDTO);
        } catch (Exception e) {
            logger.error("企业解绑银行卡回调处理,请求流水号={},发生系统异常,原因={}", response.getRequestNo(), e);
            throw e;
        }

        return result;
    }

    @Setter
    @Getter
    private static class UnbindBankCardGatewayResponse {

        private static final String CALL_SUCCESS_CODE = "0";
        private static final String BUSINESS_SUCCESS_CODE = "SUCCESS";

        // 调用状态(0 为调用成功、1 为失败,返回 1 时请看【调用失败错误码】及错误码)
        private String code;
        // 业务处理状态(处理失败 INIT;处理成功 SUCCESS),平台可根据非 SUCCESS 状态做相应处理,处理失败时可参考错误码
        private String status;
        // 错误码
        private String errorCode;
        // 错误码
        private String errorMessage;
        //平台用户编号
        private String platformUserNo;
        // 请求流水号
        private String requestNo;

        public boolean isCallSuccess() {
            return StringUtils.isNotBlank(code) && CALL_SUCCESS_CODE.equals(code);
        }

        public boolean isBusinessSuccess() {
            return StringUtils.isNotBlank(status) && BUSINESS_SUCCESS_CODE.equals(status);
        }

    }

}
